package com.handpay.nocardpay.baselibrary.setting.view.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.main.mine.MainFragment;
import com.handpay.nocardpay.baselibrary.main.widget.GestureLockDisplayView;
import com.handpay.nocardpay.baselibrary.main.widget.GestureLockView;
import com.handpay.nocardpay.baselibrary.setting.presenter.GesturePresenterImpl;
import com.handpay.nocardpay.baselibrary.setting.view.IGesturePWDChangeView;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.utils.PreferenceManager;

import java.util.HashMap;

/**
 * Created by lyang on 2017/10/11.
 */

public class GestureLockSettingFragment extends BaseFragment implements IGesturePWDChangeView {
    private GestureLockDisplayView gesture_lock_display_view;
    private GestureLockView gesture_lock_view;
    private TextView gesture_status_tv;
    private boolean isSetting = false;//是否绘制
    private int limitNum = 4;
    private GesturePresenterImpl mGesturePresenter;
    private static String GESTURE_PWD_TYPE_ADD = "1"; //.1新增
    private static String GESTURE_PWD_TYPE_CHANGE = "2";  //2修改
    private static String GESTURE_PWD_TYPE_FROM = "GESTURE_PWD_TYPE_FROM"; //判断从个页面跳转
    public static int GESTURE_PWD_TYPE_FROM_QUALIFICATION = 1; //资质提交页面
    private int mCurGesturePwdTypeFromFragment = -1;//当前页是哪个页面入

    public static GestureLockSettingFragment newInstance() {
        Bundle args = new Bundle();
        GestureLockSettingFragment fragment = new GestureLockSettingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static GestureLockSettingFragment newInstance(int from) {
        Bundle args = new Bundle();
        args.putInt(GESTURE_PWD_TYPE_FROM, from);
        GestureLockSettingFragment fragment = new GestureLockSettingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_gesture_lock;
    }

    @Override
    public void initView(View view) {
        mCurGesturePwdTypeFromFragment = this.getArguments().getInt(GESTURE_PWD_TYPE_FROM);
        gesture_status_tv = (TextView) view.findViewById(R.id.gesture_status_tv);
        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurGesturePwdTypeFromFragment == GESTURE_PWD_TYPE_FROM_QUALIFICATION) {
                    popTo(MainFragment.class, true);
                    start(MainFragment.newInstance());
                } else {
                    pop();
                }
            }
        });
        gesture_lock_display_view = (GestureLockDisplayView) view.findViewById(R.id.gesture_lock_display_view);
        gesture_lock_view = (GestureLockView) view.findViewById(R.id.gesture_lock_view);
        gesture_lock_view.setOnGestureFinishListener(new GestureLockView.OnGestureFinishListener() {
            @Override
            public void OnGestureFinish(boolean success, String key) {
                if (success) {
                    gesture_lock_display_view.setAnswer(true, key);

                    if (!isSetting) {
                        gesture_status_tv.setText(_mActivity.getString(R.string.gesture_status_new_pwd_again));
                        gesture_lock_view.setKey(key);
                        isSetting = true;
                    } else {
                        HashMap<String, Object> params = new HashMap<String, Object>();
                        String phone = AccountUtils.getAccountPhone(_mActivity);
                        params.put(BaseProtocolKey.MOBILE, SecureManager.getInstance().des(phone, 1, null));
                        params.put(BaseProtocolKey.GESTURE_PWD, SecureManager.getInstance().des(key, 1, null));
                        if (TextUtils.isEmpty(ClientEngine.getInstance().getRMSData(BaseProtocolKey.KEY_GESTURE_PWD))) {
                            params.put(BaseProtocolKey.OPERATETYPE, GESTURE_PWD_TYPE_ADD);
                        } else {
                            params.put(BaseProtocolKey.OPERATETYPE, GESTURE_PWD_TYPE_CHANGE);
                        }
                        mGesturePresenter.changeGesturePwd(params, true);
                    }
                    clearDisplayView();
                } else {
                    gesture_lock_display_view.setAnswer(false, key);
                    if (key.length() >= limitNum) {
                        showStatusAndClearDisplayView(getString(R.string.gesture_setting_pwd_fail));
                    } else {
                        showStatusAndClearDisplayView(getString(R.string.gesture_other_login_limit_tips));
                    }
                }
            }
        });
    }


    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.setting_gesture_pwd);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        isSetting = false;
        mGesturePresenter = new GesturePresenterImpl(_mActivity);
        mGesturePresenter.setGesturePwdChangeView(this);
    }

    @Override
    public void onGesturePWDSuccess() {
        PreferenceManager.getInstance(_mActivity).putBoolean(BaseProtocolKey.KEY_GESTURE_PWD_SWITCH_STATUS, true);
        DialogUtils.showToast(_mActivity, getString(R.string.gesture_setting_pwd_success));
        if (mCurGesturePwdTypeFromFragment == GESTURE_PWD_TYPE_FROM_QUALIFICATION) {
            popTo(MainFragment.class, true);
            start(MainFragment.newInstance());
        } else {
            pop();
        }
    }

    private void showStatusAndClearDisplayView(String status) {
        gesture_status_tv.setVisibility(View.VISIBLE);
        gesture_status_tv.setTextColor(getResources().getColor(R.color.red_color));
        gesture_status_tv.setText(status);
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                gesture_status_tv.setVisibility(View.VISIBLE);
                gesture_status_tv.setTextColor(getResources().getColor(R.color.text_color_normal));
                if (isSetting) {
                    gesture_status_tv.setText(getString(R.string.gesture_status_new_pwd_again));
                } else {
                    gesture_status_tv.setText(getString(R.string.gesture_status_new_pwd));
                }
                gesture_lock_display_view.clearAnswer();

            }
        };
        handler.sendEmptyMessageDelayed(1, 1000);
    }

    private void clearDisplayView() {
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                gesture_lock_display_view.clearAnswer();

            }
        };
        handler.sendEmptyMessageDelayed(1, 1000);
    }
}